package com.tz.movie.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.movie.domain.Cate;
import com.tz.movie.domain.CateExample;
import com.tz.movie.domain.User;
import com.tz.movie.domain.UserExample;
import com.tz.movie.domain.status;
 import com.tz.movie.mapper.CateMapper;
import com.tz.movie.mapper.UserMapper;

import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/cate")
public class CateController {
  @Autowired
  CateMapper catemapper;

  
  @PostMapping(value = "/getCate")
  public String getCate(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    CateExample cateExample = new CateExample();
    CateExample.Criteria criteria = cateExample.createCriteria();
    criteria.andIdEqualTo(jsonparm.getInteger("id"));
    List<Cate> Cates = catemapper.selectByExample(cateExample);

    result.put("result", status.s_ok);
    result.put("data", JSONObject.parse(Cates.get(0).toString()));
    return result.toJSONString();
  }

  @PostMapping(value = "/getAllCate")
  public String getAllUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    List<Cate> Cates = catemapper.selectByExample(new CateExample());
    JSONArray jsonArray = new JSONArray();
    for (Cate Cate : Cates) {
      JSONObject obj = JSONObject.parseObject(Cate.toString());
      jsonArray.add(obj);
    }
    result.put("result", status.s_ok);
    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/addCate")
  public String addCate(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    String name = jsonparm.getString("name");
    String info = jsonparm.getString("info");
     Cate Cate = new Cate();
    Cate.setName(name);
    Cate.setInfo(info);
    catemapper.insert(Cate);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  
  @PostMapping(value = "/updateCate")
  public String updateCate(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    int id = jsonparm.getInteger("id");

    Cate cate = new Cate();
    cate.setName(jsonparm.getString("name"));
    cate.setInfo(jsonparm.getString("info"));

    CateExample cateExample = new CateExample();
    CateExample.Criteria criteria = cateExample.createCriteria();
    criteria.andIdEqualTo(id);
    catemapper.updateByExampleSelective(cate, cateExample);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }
  
  @PostMapping(value = "/delCate")
  public String delCate(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    int delid = jsonparm.getInteger("id");
    CateExample commentExample = new CateExample();
    CateExample.Criteria criteria=  commentExample.createCriteria();
    criteria.andIdEqualTo(delid);
    catemapper.deleteByExample(commentExample);
    result.put("result", status.s_ok);

    return result.toJSONString();
  }
}